package com.springboot.cloud.sysadmin.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.dao.OperationLogMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.OperationLogQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.OperationLog;
import com.springboot.cloud.sysadmin.organization.service.IOperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@Service
@Slf4j
public class OperationLogService extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    /**
     * 新增操作日志
     *
     * @param operationLog 操作日志对象
     */
    @Override
    public boolean add(OperationLog operationLog) {
        return this.saveOrUpdate(operationLog);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param ids 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public boolean delete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            log.debug("ids:{}", ids);
            return false;
        }
        return this.removeByIds(ids);
    }

    /**
     * 查询操作日志详细
     *
     * @param id 操作ID
     * @return 操作日志对象
     */
    @Override
    public OperationLog get(String id) {
        return this.getById(id);
    }

    @Override
    public boolean getOperationLog(String id) {
        OperationLog operationLog = this.getById(id);
        if (StringUtils.isNull(operationLog)) {
            return false;
        }
        return true;
    }

    @Override
    public IPage<OperationLog> query(Page page, OperationLogQueryParam operationLogQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<OperationLog> queryWrapper = operationLogQueryParam.build();
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getTitle()), "title", operationLogQueryParam.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getMethod()), "method", operationLogQueryParam.getMethod());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getRequestMethod()), "requestMethod", operationLogQueryParam.getRequestMethod());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getOperName()), "operName", operationLogQueryParam.getOperName());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getOperUrl()), "oper_url", operationLogQueryParam.getOperUrl());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getOperIp()), "oper_ip", operationLogQueryParam.getOperIp());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getOperLocation()), "oper_location", operationLogQueryParam.getOperLocation());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getOperParam()), "oper_param", operationLogQueryParam.getOperParam());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getJsonResult()), "json_result", operationLogQueryParam.getJsonResult());
        queryWrapper.like(StringUtils.isNotBlank(operationLogQueryParam.getErrorMsg()), "error_msg", operationLogQueryParam.getErrorMsg());
        if (operationLogQueryParam.getCreatedTimeStart() != null && operationLogQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(operationLogQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(operationLogQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 清空操作日志
     */
//    @Override
//    public void cleanOperLog()
//    {
//        operationLogMapper.cleanOperLog();
//    }
    @Override
    public boolean update(OperationLog operationLog) {
        OperationLog operationLog1 = this.getById(operationLog.getId());
        if (StringUtils.isNull(operationLog1)) {
            return false;
        }
        return this.saveOrUpdate(operationLog);
    }
}
